<template>
  <div>
    <Row class="header" type="flex" justify="start">
      <Col span="16">
        <Icon class="icon" type="ios-podium-outline"/>
        <span class="title">社区来沈经过地人数前十统计</span>
      </Col>
      <Col span="8">
        <span class="note">{{startTime}} 至 {{endTime}}</span>
      </Col>
    </Row>
    <br>
    <div id="numCommunity" :style="{ width: width + 'px', height: height + 'px'}"></div>
  </div>
</template>

<script>
import echarts from 'echarts'
import { findsumAddressTypeCommunitylist } from '@/api/yi-qing-fang-kong'
// import mockData from './mockData'

export default {
  name: 'NumCommunityEchart',
  data () {
    return {
      chartData: [],
      startTime: '',
      endTime: ''
    }
  },
  props: {
    width: {
      type: Number,
      default: () => {
        return 500
      }
    },
    height: {
      type: Number,
      default: () => {
        return 300
      }
    }
  },
  mounted () {
    this.chart()
    this.init()
  },
  methods: {
    getDateStr (AddDayCount) {
      let dd = new Date()
      dd.setDate(dd.getDate() + AddDayCount)// 获取AddDayCount天后的日期
      const y = dd.getFullYear()
      const m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1)// 获取当前月份的日期，不足10补0
      const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate()// 获取当前几号，不足10补0
      return y + '-' + m + '-' + d
    },
    init () {
      this.startTime = this.getDateStr(-9)
      this.endTime = this.getDateStr(0)
    },
    chart () {
      findsumAddressTypeCommunitylist().then(ret => {
        if (ret.data.errcode === 0) {
          this.chartData = ret.data.data
          // 排序处理
          this.chartData.forEach(item => {
            item.total = item.zjwz + item.inwh + item.outwh + item.otherwh + item.datian
          })
          this.chartData.sort(function (a, b) {
            return a.total - b.total
          })
          let option = {
            title: {
              text: '',
              textStyle: {
                color: '#bababa',
                fontWeight: 500,
                fontSize: 15
              }
            },
            textStyle: {
              color: '#bababa'
            },
            grid: {
              right: '20%',
              height: '80%',
              containLabel: false
            },
            legend: {
              data: ['浙江温州', '湖北武汉', '湖北非武汉', '其它省市', '尚未设置']
            },
            tooltip: {
              trigger: 'axis',
              backgroundColor: 'rgba(50,50,50,0.6)',
              axisPointer: { // 坐标轴指示器，坐标轴触发有效
                type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
              }
            },
            dataset: {
            // dimensions: ['community', 'num'],
            // source: []
            },
            xAxis: {
              name: '单位：人',
              type: 'value',
              position: 'top',
              axisLine: {
                lineStyle: {
                  color: '#bababa' // 轴及其文字颜色
                }
              }
            },
            yAxis: {
              type: 'category',
              data: [],
              axisLabel: {
              // rotate: 40,
                interval: 0
              },
              axisLine: {
                lineStyle: {
                  color: '#bababa' // 轴及其文字颜色
                }
              }
            },
            series: [
              {
                name: '浙江温州',
                type: 'bar',
                stack: '总量',
                label: {
                  normal: {
                    show: true,
                    position: 'insideRight',
                    color: ['#fff'],
                    formatter: function (params) {
                      if (params.value > 0) {
                        return params.value
                      } else {
                        return ''
                      }
                    }
                  }
                },
                data: [],
                itemStyle: {
                // 通常情况下：
                  normal: {
                    color: ['#19968A']
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              },
              {
                name: '湖北武汉',
                type: 'bar',
                stack: '总量',
                label: {
                  normal: {
                    show: true,
                    position: 'insideRight',
                    color: ['#fff'],
                    formatter: function (params) {
                      if (params.value > 0) {
                        return params.value
                      } else {
                        return ''
                      }
                    }
                  }
                },
                data: [],
                itemStyle: {
                // 通常情况下：
                  normal: {
                    color: ['#BC3139']
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              },
              {
                name: '湖北非武汉',
                type: 'bar',
                stack: '总量',
                label: {
                  normal: {
                    show: true,
                    position: 'insideRight',
                    color: ['#fff'],
                    formatter: function (params) {
                      if (params.value > 0) {
                        return params.value
                      } else {
                        return ''
                      }
                    }
                  }
                },
                data: [],
                itemStyle: {
                // 通常情况下：
                  normal: {
                    color: ['#EA7E53']
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              },
              {
                name: '其它省市',
                type: 'bar',
                stack: '总量',
                label: {
                  normal: {
                    show: true,
                    position: 'insideRight',
                    color: ['#fff'],
                    formatter: function (params) {
                      if (params.value > 0) {
                        return params.value
                      } else {
                        return ''
                      }
                    }
                  }
                },
                data: [],
                itemStyle: {
                // 通常情况下：
                  normal: {
                    color: ['#B8A3DF']
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              },
              {
                name: '尚未设置',
                type: 'bar',
                stack: '总量',
                label: {
                  normal: {
                    show: true,
                    position: 'insideRight',
                    color: ['#fff'],
                    formatter: function (params) {
                      if (params.value > 0) {
                        return params.value
                      } else {
                        return ''
                      }
                    }
                  }
                },
                data: [],
                itemStyle: {
                // 通常情况下：
                  normal: {
                    color: ['#96BFFF']
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              }
            ]
          }
          this.chartData.forEach(item => {
          // option.dataset.source.push(item)
            option.yAxis.data.push(item.community)
            option.series[0].data.push(item.zjwz)
            option.series[1].data.push(item.inwh)
            option.series[2].data.push(item.outwh)
            option.series[3].data.push(item.otherwh)
            option.series[4].data.push(item.datian)
          })
          let myChart = echarts.init(document.getElementById('numCommunity'))
          myChart.setOption(option)
        } else {
          this.$Message.error({
            content: '数据获取失败,\n' + ret.data.errmsg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .header {
    height: 30px;
  }

  .icon {
    font-size: 28px;
    font-weight: 600;
    margin-right: 5px;
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    line-height: 30px;
  }
  .note {
    font-size: 15px;
    line-height: 30px;
  }
</style>
